package com.danskin.wechat.webapi;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.danskin.wechat.entity.CustItemRecord;
import com.danskin.wechat.entity.CustOrderDetail;
import com.danskin.wechat.entity.CustOrderRecord;
import com.danskin.wechat.entity.CustPointRecord;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.model.CustItemRDTO;
import com.danskin.wechat.model.CustOrderDetailDTO;
import com.danskin.wechat.model.CustOrderRecordDTO;
import com.danskin.wechat.service.CustItemRecordService;
import com.danskin.wechat.service.CustOrderRecordService;
import com.danskin.wechat.service.CustPointRecordService;
import com.danskin.wechat.util.ConvertUtils;
import com.danskin.wechat.util.Page;
/***
 * 消费记录列表
 * @author admin
 *
 */
@RestController
@RequestMapping("api/points")
public class PointsController extends BaseApiController{
	@Resource
	private CustOrderRecordService custOrderRecordService;
	@Resource
	private CustItemRecordService  custItemRecordService;
	/***
	 * 消费记录列表
	 * @param appId
	 * @param custInfo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getCustOrderRecord/{appId}",method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object getCustOrderRecord(@PathVariable("appId") String appId,@RequestBody Map<String, Object> map, HttpServletRequest request) {
		if (!map.containsKey("pageIndex")) {
			return this.getError("pageIndex不能为空");
		}else if (!map.containsKey("openId")) {
			return this.getError("openId不能为空");
		}
		String sPageIndex= map.get("pageIndex").toString();
		int pageIndex = ConvertUtils.getInt(sPageIndex);
		int pageSize = 10;
		map.put("appId", appId);
		Page page = new Page();
		page.setCurrentResult(pageIndex);
		page.setShowCount(pageSize);
		page.setMap(map);
		List<CustItemRecord> listRecord = custItemRecordService.queryCustItemRecord(page);
		List<CustItemRDTO> listDto = new ArrayList<CustItemRDTO>();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(listRecord.size()>0){
			for(CustItemRecord itemRecord:listRecord){
				CustItemRDTO dto = new CustItemRDTO();
				dto.setId(String.valueOf(itemRecord.getId()));
				dto.setOrderNo(itemRecord.getOrderNo());
				dto.setCreatedTime(fmt.format(itemRecord.getCreatedTime()));
				dto.setTotalPoints(String.valueOf(itemRecord.getTotalPoints()));
				listDto.add(dto);
			}
		}
		return new AccessResponse<Object>("0", "ok", listDto);
	}
	/***
	 * 订单消费记录信息 
	 * @param appId
	 * @param custInfo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getCustOrderDetail/{appId}",method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object getCustOrderDetail(@PathVariable("appId") String appId,@RequestBody Map<String, Object> map, HttpServletRequest request) {
		SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (!map.containsKey("openId")) {
			return this.getError("openId不能为空");
		}else if (!map.containsKey("orderNo")) {
			return this.getError("orderNo不能为空");
		}
		String openId = map.get("openId").toString();
		String orderNo = map.get("orderNo").toString();//订单号
		CustOrderRecord cust  = custOrderRecordService.findCustOrderRecord(appId,openId,orderNo);
		List<CustOrderDetail> listDetail = null;
		CustOrderRecordDTO dto = new CustOrderRecordDTO();
		if(cust!=null){
			listDetail =  custOrderRecordService.queryCustOrderDetail(cust.getOrderNo());	
			dto.setSerialNum(cust.getOrderNo());
			dto.setSerialTime(fmt.format(cust.getTransTime()));
			dto.setSerialStatus("已完成");
			dto.setShopNum(cust.getStoreCode());
			dto.setIntegral(String.valueOf(cust.getGetPoints()));
			dto.setFrom(cust.getSource());
			dto.setProductList(convCustOrderDetail(listDetail));
		}
		return new AccessResponse<Object>("0", "成功", dto);
	}
	/***
	 * 消费记录详细记录转化
	 * @param listDetail
	 * @return
	 */
	public List<CustOrderDetailDTO> convCustOrderDetail(List<CustOrderDetail> listDetail){
		List<CustOrderDetailDTO> listDTO = new ArrayList<CustOrderDetailDTO>();
		if(listDetail.size()>0){
			for(CustOrderDetail de:listDetail){
				CustOrderDetailDTO dto = new CustOrderDetailDTO();
				dto.setProductName(de.getProductName());
				dto.setProPrice(String.valueOf(de.getPrice()));
				dto.setProductNum(String.valueOf(de.getQuantity()));
				dto.setProductSize(de.getSize());
				dto.setProductColor(de.getColorName());
				listDTO.add(dto);
			}
		}
		return listDTO;
	}
}
